﻿using LaunchAllaround.DesignPattern.MVC;
using LaunchAllaround.Sound;
using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LaunchAllaround.Key
{
    /// <summary>
    /// Logique d'interaction pour KeyUI.xaml
    /// </summary>
    public partial class KeyUI : UserControl, View<KeyModel>, Controller<KeyModel>
    {

        private Model<KeyModel> model;

        /**
         * Constructeur par défaut 
         **/
        public KeyUI()
        {
            this.model = new KeyModel(this);
            InitializeComponent();
            this.VolumeSlider.Value = Math.Round(((KeyModel)this.model).Volume);
            songPath.Text = this.FormatPath(((KeyModel)this.model).Path);
        }

        public KeyUI(KeyModel model)
        {
            this.model = model;
            InitializeComponent();
            this.VolumeSlider.Value = Math.Round(((KeyModel)this.model).Volume);
            songPath.Text = this.FormatPath(((KeyModel)this.model).Path);
        }


        public void Refresh(RefreshEvent<KeyModel> ev)
        {
            KeyModel k = (KeyModel)ev.Src;
            if (ev is RefreshPath)
            {
                songPath.Text = this.FormatPath(k.Path);
            }
            else if (ev is RefreshPreview)
            {
                this.preview.Visibility = Visibility.Visible;
                this.Volume.Visibility = Visibility.Visible;
            }
            else if (ev is RefreshVolume)
            {
                this.VolumeSlider.Value = Math.Round(k.Volume);
            }
            
            
        }

        public Model<KeyModel> GetModel()
        {
            return this.model;
        }

        public void SetModel(Model<KeyModel> m)
        {
            this.model = m;
        }

        private void SongPathChangeEvent(object sender, MouseButtonEventArgs e)
        {
            var ofd = new Microsoft.Win32.OpenFileDialog() { Filter = "Music files|*.mp3;*.wav;*.aiff|mp3 files (*.mp3)|*.mp3|wav files (*.wav)|*.wav|aiff files (*.aiff)|*.aiff" };
            var result = ofd.ShowDialog();
            if (result == false) return;
            songPath.Text = this.FormatPath(ofd.FileName);
            ((KeyModel)this.GetModel()).Path = ofd.FileName;
        }

        private void PreviewButtonChangeEvent(object sender, MouseButtonEventArgs e)
        {
            string path = ((KeyModel)this.model).Path;
            if (SongPlayer.myself.IsAValidSong(path))
            {
                SongPlayer sp = SongPlayer.myself;
                sp.Play(new Song(((KeyModel)this.model).Path, ((KeyModel)this.model).Volume));
            }
        }

        private void SliderValueChangedEvent(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            double newValue = Math.Round(e.NewValue);
            this.VolumeText.Content = newValue.ToString();
            this.VolumeSlider.SelectionEnd = newValue;
            ((KeyModel)this.model).Volume = (float)(this.VolumeSlider.Value);
            this.RefreshVolumeImage(newValue);

        }

        private void RefreshVolumeImage(double volume)
        {
            if (volume == 0)
            {
                this.VolumeImage.Source = new BitmapImage(new Uri("/Resources/Images/Themes/Dark/Sound/volume-disable.png", UriKind.Relative));
            }
            else if ((volume < 33) && (volume > 0))
            {
                this.VolumeImage.Source = new BitmapImage(new Uri("/Resources/Images/Themes/Dark/Sound/volume-low.png", UriKind.Relative));
            }
            else if ((volume < 66) && (volume > 33))
            {
                this.VolumeImage.Source = new BitmapImage(new Uri("/Resources/Images/Themes/Dark/Sound/volume-moderate.png", UriKind.Relative));
            }
            else if ((volume <= 100) && (volume > 66))
            {
                this.VolumeImage.Source = new BitmapImage(new Uri("/Resources/Images/Themes/Dark/Sound/volume-high.png", UriKind.Relative));
            }
        }

        private string FormatPath(string path)
        {
            string[] pathT = path.Split('\\');
            return pathT[pathT.Length - 1];
        }

    }


}
